package com.xhsj.user.sms.purchase.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.sms.base.BaseService;
import com.xhsj.user.sms.purchase.entity.TbPurchaseTerminationContractApplyFile;
import com.xhsj.user.sms.purchase.mapper.TbPurchaseTerminationContractApplyFileMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 终止合同记录文件	 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since 2020-05-26
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbPurchaseTerminationContractRecordFile")
public class TbPurchaseTerminationContractApplyFileService extends BaseService<TbPurchaseTerminationContractApplyFileMapper, TbPurchaseTerminationContractApplyFile> {
    @Autowired
    private TbPurchaseTerminationContractApplyFileMapper dao;

    @Override
    //@CacheEvict(cacheNames="TbPurchaseTerminationContractRecordFile", allEntries=true)
    public boolean save(TbPurchaseTerminationContractApplyFile entity) {
        return super.save(entity);
    }

    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbPurchaseTerminationContractApplyFile getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    //@CacheEvict(cacheNames="TbPurchaseTerminationContractRecordFile", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    //@Cacheable(key = "#data",sync = true)
    public IPage<TbPurchaseTerminationContractApplyFile> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbPurchaseTerminationContractApplyFile entity = JSON.parseObject(JSON.toJSONString(data), TbPurchaseTerminationContractApplyFile.class);
        Page<TbPurchaseTerminationContractApplyFile> page = new Page(currentPage,pageSize);
        QueryWrapper<TbPurchaseTerminationContractApplyFile> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

    @Override
    //@Cacheable(value = "TbPurchaseTerminationContractRecordFile",sync = true)
    public List<TbPurchaseTerminationContractApplyFile> selectAll() {
        return super.selectAll();
    }


    //删除回执文件
    public int removeFile(String contractId) {
       return dao.removeFile(contractId);
    }

    //获取终止合同申请相关数据
    public List<Map<String, Object>> getTerminationContractList(String dataId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("termination_contract_id",dataId);
       return dao.selectMaps(queryWrapper);
    }

    public int deleteFileDb(String fileId) {
        return dao.deleteFileDb(fileId);
    }
}
